package com.aruba.drawerapplication;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.widget.DrawerLayout;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;

/**
 * 自定义DrawerLayout，里面的自定义LinearLayout自动包裹一层
 */
public class MyDrawerLayout extends DrawerLayout implements DrawerLayout.DrawerListener {
    //内容view
    private View contentView;
    //实际上的侧滑view
    private SlideRelativeLayout slideRelativeLayout;
    private float fraction;
    private float touchY;

    public MyDrawerLayout(@NonNull Context context) {
        this(context, null);
    }

    public MyDrawerLayout(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        //添加侧滑监听
        addDrawerListener(this);
    }

    @Override
    public void addView(View child, ViewGroup.LayoutParams params) {
        //偷梁换柱
        if (child instanceof SlideLinearLayout) {
            slideRelativeLayout = new SlideRelativeLayout(getContext());
            slideRelativeLayout.attachLinearLayout((SlideLinearLayout) child, params);

            //将drawerLayout生成的LayoutParams设置给自身
            super.addView(slideRelativeLayout, params);
        } else {
            contentView = child;
            super.addView(child, params);
        }
    }

    /**
     * 这里将触摸的y值分发下去
     *
     * @param ev
     * @return
     */
    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        this.touchY = ev.getY();

        if (ev.getAction() == MotionEvent.ACTION_UP) {
            slideRelativeLayout.onMotionUp(ev.getRawX(), ev.getRawY());
            if (fraction >= 1) {
                slideRelativeLayout.setOpen(true);
            } else {
                slideRelativeLayout.setOpen(false);
            }
        }

        //没有打开之前 不拦截 我们在onDrawerSlide回调中处理   
        if (fraction < 1) {
            return super.dispatchTouchEvent(ev);
        } else {//等于1后 onDrawerSlide将不会回调  内容区域不再进行偏移,但是背景的贝塞尔曲线还要随手指触摸的点变化
            if (!slideRelativeLayout.isOpen()) {
                slideRelativeLayout.setTouchY(touchY, fraction);
                return true;
            } else {
                return super.dispatchTouchEvent(ev);
            }
        }
    }


    /**
     * 这里记录打开比例
     *
     * @param view
     * @param v
     */
    @Override
    public void onDrawerSlide(@NonNull View view, float v) {
        this.fraction = v;
        slideRelativeLayout.setTouchY(touchY, fraction);
        //针对内容区域进行偏移
        float contentViewoffset = getWidth() * fraction / 2;
        contentView.setTranslationX(contentViewoffset);
    }

    @Override
    public void onDrawerOpened(@NonNull View view) {
        slideRelativeLayout.setOpen(true);
    }

    @Override
    public void onDrawerClosed(@NonNull View view) {
        fraction = 0;
        slideRelativeLayout.setOpen(false);
    }

    @Override
    public void onDrawerStateChanged(int i) {

    }

    public void setItemClickListener(SlideLinearLayout.ItemClickListener itemClickListener) {
        if (slideRelativeLayout != null) {
            slideRelativeLayout.setItemClickListener(itemClickListener);
        }
    }
}
